using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Threading;

namespace GoogleNet
{
    /// <summary>
    /// Positioning coordinates set for overlay elements.
    /// </summary>
    public class KmlXY : IKmlElement
    {
        private string tagName = "XY";

        #region X
        private double x;

        /// <summary>
        /// Either the number of pixels, a fractional component of the image, 
        /// or a pixel inset indicating the x component of a point on the overlay image. 
        /// </summary>
        /// <value>The X.</value>
        public double X
        {
            get { return x; }
            set { x = value; }
        } 
        #endregion

        #region Y
        private double y;

        /// <summary>
        /// Either the number of pixels, a fractional component of the image, 
        /// or a pixel inset indicating the y component of a point on the overlay image. 
        /// </summary>
        /// <value>The Y.</value>
        public double Y
        {
            get { return y; }
            set { y = value; }
        } 
        #endregion

        #region XUnits
        private KmlXYCoordinatesType xUnits = KmlXYCoordinatesType.fraction;

        /// <summary>
        /// Gets or sets the X units type.
        /// </summary>
        /// <value>The X units.</value>
        public KmlXYCoordinatesType XUnits
        {
            get { return xUnits; }
            set { xUnits = value; }
        } 
        #endregion

        #region YUnits
        private KmlXYCoordinatesType yUnits = KmlXYCoordinatesType.fraction;

        /// <summary>
        /// Gets or sets the Y units type.
        /// </summary>
        /// <value>The Y units.</value>
        public KmlXYCoordinatesType YUnits
        {
            get { return yUnits; }
            set { yUnits = value; }
        } 
        #endregion

        
        #region IKmlElement Members

        /// <summary>
        /// Name of the KML tag for this specific element.
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        public string TagName
        {
            get { return tagName; }
        }

        #endregion

        #region IKmlGenerator Members

        /// <summary>
        /// Generates the KML for this element including its child elements.
        /// </summary>
        /// <param name="writer">The XML writer that will be used to generate the KML.</param>
        public void GenerateKml(System.Xml.XmlTextWriter writer)
        {
            CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");

            writer.WriteAttributeString("x", x.ToString(Thread.CurrentThread.CurrentCulture.NumberFormat));
            writer.WriteAttributeString("y", y.ToString(Thread.CurrentThread.CurrentCulture.NumberFormat));
            writer.WriteAttributeString("xunits", xUnits.ToString());
            writer.WriteAttributeString("yunits", yUnits.ToString());
            
            Thread.CurrentThread.CurrentCulture = currentCulture;
        }

        #endregion
    }
}
